package cn.com.callback

import cn.com.callback.ServiceBackInvoke;
import cn.com.elite.EliteBean;
import cn.com.elite.Util;
import cn.com.kh.KHDBConfig
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import cn.com.kh.KHUtil

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse

import net.sf.json.JSONObject;

/**
 *
 * @Title: 创建三高工单接口
 * @Author: Rick*
 * @Date 2022/9/7
 *
 */
public class apiCrateWork implements ServiceBackInvoke {
    private static final Log log = LogFactory.getLog(apiCrateWork.class);

    public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {

        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=utf-8");
        // 初始化参数
        String apipram = "";
        JSONObject resultJson = new JSONObject();
        //根据不同的调用方式获取参数
        String getMethod = request.getMethod();
        if (getMethod.equals("GET")) {
            apipram = request.getQueryString();
            log.info("接收调用GET参数信息：" + apipram);
        } else if (getMethod.equals("POST")) {
            InputStreamReader reader = new InputStreamReader(request.getInputStream(), "UTF-8");
            BufferedReader iny = new BufferedReader(reader);
            String line;
            while ((line = iny.readLine()) != null) {
                apipram += line;
            }
            log.info("接收调用POST报文信息：" + apipram);
        }

        //参数定义
        String Re_customerNo, Re_contactName, Re_contactPhone, Re_happenedAddress, Re_receiptComment, Re_szh_workid, Re_staff_id, Re_rolegroup_id, Re_CommitmentDeadline, Re_ReceiptCommentComment;
        //参数处理
        JSONObject inJson = JSONObject.fromObject(apipram);
        Re_ReceiptCommentComment = KHUtil.GetSFJV(inJson, "ReceiptCommentComment");     //案件号
        Re_szh_workid = KHUtil.GetSFJV(inJson, "szh_workid");     //数字化ID
        Re_staff_id = KHUtil.GetSFJV(inJson, "staff_id");     //创建人
        Re_rolegroup_id = KHUtil.GetSFJV(inJson, "rolegroup_id");     //创建组
        Re_customerNo = KHUtil.GetSFJV(inJson, "customerNo");     //客户编号，无客户编号
        Re_contactName = KHUtil.GetSFJV(inJson, "contactName");     //客户姓名
        Re_contactPhone = KHUtil.GetSFJV(inJson, "contactPhone");     //客户手机
        Re_happenedAddress = KHUtil.GetSFJV(inJson, "happenedAddress");     //客户地址
        Re_receiptComment = KHUtil.GetSFJV(inJson, "receiptComment");     //备注
        Re_CommitmentDeadline = KHUtil.GetSFJV(inJson, "CommitmentDeadline");     //完成时限
        //校验传参信息
        if (Re_szh_workid.equals("") || Re_receiptComment.equals("")) {
            resultJson.put("resultcode", "-1");
            resultJson.put("resultmessage", "未传递正确参数！");
            return resultJson;
        }

        //校验通过，开始执行调用
        return HttpCreateWork(eliteBean, Re_szh_workid, Re_customerNo, Re_contactName, Re_contactPhone, Re_happenedAddress, Re_receiptComment, Re_staff_id, Re_rolegroup_id, Re_CommitmentDeadline, Re_ReceiptCommentComment)
    }

    //三高工单创建接口调用
    private static JSONObject HttpCreateWork(EliteBean eliteBean, String Re_szh_workid, String Re_customerNo, String Re_contactName, String Re_contactPhone, String Re_happenedAddress, String Re_receiptComment, Re_staff_id, Re_rolegroup_id, Re_CommitmentDeadline, Re_ReceiptCommentComment) {
        JSONObject resultJson = new JSONObject();
        //校验接口地址
        String CREATE_URL = KHDBConfig.getAPIURL(eliteBean, "3H_createReceiptEasyMode");
        if (CREATE_URL.equals("") || CREATE_URL.equals("null")) {
            resultJson.put("resultcode", "-2");
            resultJson.put("resultmessage", "未配置接口地址！");
            return resultJson;
        }

        try {
            //生成头部
            Map<String, String> headMap = new HashMap<String, String>();
            headMap.put("Content-Type", "application/json;");
            //生成请求内容
            JSONObject infojson = new JSONObject();
            infojson.put("infoSourceId", 88);
            infojson.put("csReceiptTypeId", 1);
            infojson.put("csServiceCatalogId", 15);
            infojson.put("customerNo", Re_customerNo);
            if(!Re_contactName.equals("")&&!Re_contactPhone.equals("")){
                    infojson.put("contactName", Re_contactName);
                    infojson.put("contactPhone", Re_contactPhone);
                }
            infojson.put("happenedAddress", Re_happenedAddress);
            infojson.put("receiptComment", Re_receiptComment);
            infojson.put("caseNo", Re_szh_workid);
            infojson.put("ReceiptCommentComment", Re_ReceiptCommentComment);
            infojson.put("CommitmentLevel", 2);
            infojson.put("CommitmentDeadline", Re_CommitmentDeadline);
            log.debug("三高工单创建接口报文:" + infojson.toString());

            String resultpost = Util.doHttpPostUseHead(CREATE_URL, infojson.toString(), "UTF-8", 10000, 0, headMap);
            log.debug("三高工单创建接口返回:" + resultpost);
            //解析创建工单接口返回信息
            try {
                JSONObject resultpostJson = JSONObject.fromObject(resultpost);
                String data_string = KHUtil.GetSFJV(resultpostJson, "data");//获取data数据
                JSONObject data_json = JSONObject.fromObject(data_string);
                String sg_workid = KHUtil.GetSFJV(data_json, "ReceiptNo");//三高返回工单ID
                if (sg_workid.equals("")) {
                    resultJson.put("resultcode", "-3");
                    resultJson.put("resultmessage", "三高接口调用后错误，或者未返回三高工单ID！");
                    return resultJson;
                } else {
                    //写入关联数据
                    eliteBean.update("insert into SW_workorderlink(link_guid,szh_workid,sg_workid,createdate,staff_id,rolegroup_id) values(NEWID(),'" + Re_szh_workid + "','" + sg_workid + "',getdate(),'" + Re_staff_id + "','" + Re_rolegroup_id + "')", "dataSource");

                    resultJson.put("resultcode", "0");
                    resultJson.put("resultworkorderid", sg_workid);
                    resultJson.put("resultmessage", "成功创建三高工单，工单ID为：" + sg_workid);
                }
                return resultJson;
            } catch (Exception e) {
                log.error("接口数据处理错误", e);
                resultJson.put("resultcode", "-5");
                resultJson.put("resultmessage", "接口数据处理错误:" + resultpost);
                resultJson.put("data", "");
                return resultJson;
            }

        } catch (Exception e) {
            log.error("数据处理错误", e);
            resultJson.put("resultcode", "-4");
            resultJson.put("resultmessage", "调用失败:" + e);
            resultJson.put("data", "");
            return resultJson;
        }

    }

}
